package com.fept.msgcore.scheduler.job.base;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Srma on 2/10/2017.
 */
public class NonConcurrentScheduleJob extends BaseScheduleJob {
    static Map<Class<?>, ReentrantLock> locks = new HashMap();

    public NonConcurrentScheduleJob() {
    }

    public String getTimeExpression() {
        return "0 0/5 * * * ?";
    }

    protected void concurrentScheduleJobDetected() {
        logger.error("concurrent detected on NonConcurrentScheduleJob");
        throw new RuntimeException("concurrent detected on non concurrent schedule job");
    }

    private ReentrantLock getLock() {
        Class var1 = NonConcurrentScheduleJob.class;
        synchronized(NonConcurrentScheduleJob.class) {
            if(!locks.containsKey(this.getClass())) {
                locks.put(this.getClass(), new ReentrantLock());
            }

            return (ReentrantLock)locks.get(this.getClass());
        }
    }

    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            ReentrantLock e = this.getLock();
            if(!e.tryLock()) {
                try {
                    this.concurrentScheduleJobDetected();
                } catch (Exception var9) {
                    logger.error("concurrent detected on NonConcurrentScheduleJob");
                    logger.error(var9.getMessage());
                }

                return;
            }

            try {
                this.doExecute(context);
            } catch (Exception var10) {
                logger.error(var10.getMessage());
            } finally {
                e.unlock();
            }
        } catch (Exception var12) {
            logger.error("exception detected on NonConcurrentScheduleJob");
            logger.error(var12.getMessage());
        }

    }

    public void process() throws SchedulerException
    {

    }
}
